// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    traceUser: true,
    env: 'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const tripId = event.tripId
    const tripInfoRaw = await db.collection('trip').where({
        _id: tripId
    }).get()
    const tripInfo = tripInfoRaw.data[0]
    console.log(tripInfo)
    var daysInfo =[]

    for (var i = 0; i<tripInfo.days.length; i++) {
        var dayId = tripInfo.days[i]
        
        var dateRaw = new Date(tripInfo.tripBeginTime + (1 * 24 * 60 * 60 * 1000) * i)
        var year = dateRaw.getFullYear(); //得到年份
        var month = dateRaw.getMonth() + 1; //得到月份
        var date = dateRaw.getDate(); //得到日期

        await db.collection('tripDays').where({
            _id: dayId
        }).get().then(res => {
            var dayInfo = res.data[0]
            var dateRaw = new Date(tripInfo.tripBeginTime + (1 * 24 * 60 * 60 * 1000) * i)
            var year = dateRaw.getFullYear(); //得到年份
            var month = dateRaw.getMonth() + 1; //得到月份
            var date = dateRaw.getDate(); //得到日期
            daysInfo.push({
                dayInfo,year,month,date
            })
        })
    }
    
    return {
        tripId: tripInfo._id,
        title: tripInfo.title,
        daysInfo,
        tripBeginTime: tripInfo.tripBeginTime,
        tripEndTime: tripInfo.tripEndTime
    }
}